package demo1;

import java.util.List;

public class MyListStack {
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;

    public int push(int val) {
        ListNode cur=new ListNode(val);
        if(head==null){
            head=cur;
        }else{
            cur.next=head;
            head=cur;
        }
        return head.val;
    }

    public int pop() {
        if(head==null){
            throw new EmptyException();
        }
        ListNode cur=head;
        head=head.next;
        return cur.val;
    }

    public int peek() {
        if(head==null){
            throw new EmptyException();
        }
        return head.val;
    }

    public int size() {
        ListNode cur = head;
        int size = 0;
        while (cur != null) {
            size++;
            cur = cur.next;
        }
        return size;
    }

    public boolean empty() {
        if (head == null) {
            return false;
        }
        return true;
    }

}
